package edu.isquen.util.xml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;

import com.thoughtworks.xstream.XStream;

import edu.isquen.core.user.model.User;
import edu.isquen.core.user.service.UserService;
import edu.isquen.util.factory.ServiceFactory;

public class XMLUtil {

	public static void write(Object obj,File file) throws Exception {
		XStream sm =new XStream();
		FileOutputStream ops =new FileOutputStream(file);
		sm.toXML(obj, ops);
		ops.close();
		
		
	}
	
	public static User getUserForTest(String name,String password) throws Exception{
		User user=null;
		UserService userService = (UserService) ServiceFactory.createProcess(UserService.class);
		user=userService.doLogin(name,password);
		return user;
	}
	
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		User user=getUserForTest("admin", "123456");
		File file=new File("c:\\user.xml");
		write(user,file);
		read(file);
	}

	public static Object read(File file) throws Exception{
		XStream sm =new XStream();
		FileReader freader=new FileReader(file);
		Object user=sm.fromXML(freader);
		System.out.println("user-->"+user);
		freader.close();
		return user;
	}
	
}
